package com.china08.yxyapi.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Pattern;

public class SchNumGenerator {
    /**
     * 英文简写（默认）如：20101201
     */
    public static String FORMAT_SHORT_YMD = "yyyyMMdd";
    public static String FORMAT_LONG_YMD = "yyyy-MM-dd";

    public Date intToDate(Integer dd) {
        SimpleDateFormat df = new SimpleDateFormat(SchNumGenerator.FORMAT_SHORT_YMD);
        try {
            Date date = df.parse("20170611");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getDateString(Date date) {
        SimpleDateFormat df = new SimpleDateFormat(SchNumGenerator.FORMAT_SHORT_YMD);
        Calendar calendar = Calendar.getInstance();
        if (date != null) {
            calendar.setTime(date);
        }
        return df.format(calendar.getTime());
    }

    /**
     * 自增序号
     * 
     * @param num
     * @return
     */
    public static String schNumGenerator(String num) {
        StringBuffer sch = new StringBuffer();
        sch.append(SchNumGenerator.getDateString(new Date()));
        if ((num.trim().length() == 0) || !SchNumGenerator.isNumber(num)) return sch.append("001").toString();
        num = String.valueOf(Integer.valueOf(num) + 1);
        int key = num.trim().length();
        switch (key) {
        case 1:
            num = sch.append("00").append(num).toString();
            return num;
        case 2:
            num = sch.append("0").append(num).toString();
            return num;
        case 3:
            return sch.append(num).toString();
        }
        return sch.append(num).toString();
    }

    /**
     * 学段\学科\学年 序号自增器
     * 
     * @param num
     * @param xd
     * @return
     */
    public static String xnumGenerator(String num, String xd) {
        StringBuffer sch = new StringBuffer();
        xd = xd.toUpperCase(); // 转大写
        // sch.append(SchNumGenerator.getDateString(new Date()));
        sch.append(xd);
        if ((num.trim().length() == 0) || !SchNumGenerator.isNumber(num)) return sch.append("0001").toString();
        num = String.valueOf(Integer.valueOf(num) + 1);
        int key = num.trim().length(); // 位数
        switch (key) {
        case 1:
            num = sch.append("000").append(num).toString();
            return num;
        case 2:
            num = sch.append("00").append(num).toString();
            return num;
        case 3:
            return sch.append("0").append(num).toString();
        case 4:
            return sch.append(num).toString();
        }
        return null;
    }

    /**
     * 字符匹配
     * 
     * @param num
     * @return
     */
    public static boolean isNumber(String num) {
        String reg = "^[0-9]*[1-9][0-9]*$";
        return Pattern.compile(reg).matcher(num).find();
    }

    /**
     * 正则判断是否为数字
     */
    public static Pattern patternInt = Pattern.compile("(^[+]?([0-9]|([1-9][0-9]*)))");

    public static boolean IntegerUtil(String num) {
        if (num == null) { return false; }
        return patternInt.matcher(num).matches();
    }
}
